﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sale_OrderHistory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridViewOrders_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index;
        string orderid;

        if ("ViewEdit" == e.CommandName)
        {
            index = PublicDefine.GetSelectRowIndex(e);
            orderid = GridViewOrders.Rows[index].Cells[0].Text;
            Response.Redirect("NewSaleOrder.aspx?OrderID=" + orderid);
        }
        else if ("DeleteOrder" == e.CommandName)
        {
            index = PublicDefine.GetSelectRowIndex(e);
            orderid = GridViewOrders.Rows[index].Cells[0].Text;
            DeleteOrder(int.Parse(orderid));
            GridViewOrders.DataBind();
        }
    }

    void DeleteOrder(int orderid)
    {
        using (SqlConnection connect = new SqlConnection(PublicDefine.SQLConnectString))
        {
            connect.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = connect;
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter parameter;
            cmd.CommandText = "sp_Order_Delete";
            cmd.Parameters.Clear();
            parameter = new SqlParameter("@OrderID", orderid);
            cmd.Parameters.Add(parameter);
            cmd.ExecuteNonQuery();
            connect.Close();
        }

    }

}
